clc; clear; close all;
load('intermediate\CompileAllCIPCalcsFreeBeta.mat');
addpath('figures', 'functions')

%% Set figure options

optfig.fontname     = 'Times New Roman';
optfig.dimension    = [0 0 8 6];
optfig.lw           = 2;
optfig.folder       = 'output\FreeBetas\';
optfig.color        = num2cell(parula(7),2);
% optfig.color      = num2cell(jet(7),2);
optfig.style        = {'-','--',':','-.','-','--','-',};
optfig.marker       = {'none','o','x','s','+'};
optfig.markersize   = 10;

optfig.fontsize_tit = 22;
optfig.fontsize_ax  = 22; %46
optfig.fontsize_lab = 20;
optfig.fontsize_leg = 16;

optfig.plotfig = 1; % If 1, plots figures

%% Create figures

fig_price_impact_curves(valsForSumStats2, alphas, Multipliers, TickSizes, optfig)
fig_FX_futures_price_impact(datesExtras, selectorNonHolidays, coeffs, optfig)
fig_CIP_history(dateVecCIP2, CIPCalcsAll, optfig)
fig_CIP_arbsize(datesExtras, selectorNonHolidays, WelfareArbs, optfig)
fig_CIP_welfare(datesExtras, selectorNonHolidays, WelfareArbs, optfig)
fig_CIP_welfare_history_2010(CIPAllS1, WelfareArbs2, optfig)



%Compute Isoquants and generate figure
CIPVec2 = CIPCalcEUR(3077:end,:);
selectorCIP = zeros(315,1)+1;
selectorCIP([8 9 13 65 85 130 195 260 270 275],:) = 0;
selectorCIP = (selectorCIP>0);
%Create a selector vector that eliminates holidays and select out data
CIPVec2 = CIPVec2(selectorCIP,:);
alphaGrid = (0.002:0.001:0.051)';
CCBEuro = zeros(50,3);
counter = 4; %Select the Euro
alphacounter = 0;
for welfareSize=1:3
    parfor(alphacounter=1:size(alphaGrid,1))
        options = optimoptions('fsolve','FunctionTolerance',1e-12,'OptimalityTolerance',1e-12,'StepTolerance',1e-12);
        fun = @(c) (WelfareSearcher(c,alphaGrid(alphacounter,1),1)+10*10^(welfareSize))^2;
        theccb = fsolve(fun,-0.01,options);
        CCBEuro(alphacounter,welfareSize) = theccb;
    end
end

fig_isoquants(alphaGrid*10, CCBEuro,alphas(1,4),max(coeffs(:,4)),min(CIPVec2),mean(CIPVec2),optfig)

%% Scatter

parfor (counter=1:5)
    fig_CIP_ccbVSalphasScatter(CIPCalcsAll,coeffs,counter,optfig)
end

estimatesForFreeBetaTable = zeros(4,5);
counter = 0;
while(counter<5)
    counter = counter+1;
    estimatesForFreeBetaTable(1,counter)=fullCoeffs_FreeBetaComposite{1,counter}(2,1);
    estimatesForFreeBetaTable(2,counter)=fullCoeffs_FreeBetaCompositeStError{1,counter}(1,2);
    estimatesForFreeBetaTable(3,counter)=fullCoeffs_FreeBetaComposite{1,counter}(3,1);
    estimatesForFreeBetaTable(4,counter)=fullCoeffs_FreeBetaCompositeStError{1,counter}(1,3);
end

%Plot Free Beta Estimates

%Initialize for storage
alphaFrees = zeros(size(fullCoeffs_FreeBetaDaily{1,1},1),5);
betaFrees = zeros(size(fullCoeffs_FreeBetaDaily{1,1},1),5);
%Store in matrices
counter = 0;
while(counter<5)
    counter = counter+1;
    alphaFrees(:,counter) = fullCoeffs_FreeBetaDaily{1,counter}(:,2); 
    betaFrees(:,counter) = fullCoeffs_FreeBetaDaily{1,counter}(:,3); 
end
fig_FX_futures_price_impact_freebetaAlphas(datesExtras, selectorNonHolidays, alphaFrees, optfig)
fig_FX_futures_price_impact_betas(datesExtras, selectorNonHolidays, betaFrees, optfig)


%{
parfor (counter=1:5)
    fig_CIP_ccbVSalphasScatter(CIPCalcsAll,alphaFrees,counter,optfig)
end

parfor (counter=1:5)
    fig_CIP_ccbVSfreeBetaalphasScatter(CIPCalcsAll,alphaFrees,counter,optfig)
    fig_CIP_ccbVSfreeBetabetasScatter(CIPCalcsAll,betaFrees,counter,optfig)
    fig_CIP_ccbVSbetasScatter(CIPCalcsAll,betaFrees,counter,optfig)
end
%}

save('intermediate\PriceImpactFreeBetaEstimatesForTable','estimatesForFreeBetaTable','estimatesForFreeBetaTable');